using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Nsb;
using Nsb.Classes;
using ThemeChain;
using jQueryApi;

namespace Loa
{
    public static class TimerPage
    {
        public static jQueryObject Indicator;
        public static bool QuarterBell = true;
        public static bool Running;
        public static TimerGadget Timer;
        public static int Waiter;
        public const string QuarterBellKey = "TmrPgQtrBell";
        public static jQueryObject Sound0;
        public static jQueryObject Sound1;
        public static jQueryObject Sound2;
        public static jQueryObject Sound4;

        public const string BellSound = "http://www.rangde.org/static/bell-ring-01.mp3";
        public const string ClinkSound = "http://soundjax.com/reddo/31398%5Eglass-clink-3.mp3";
        public const string WhiperwillSound = "http://soundjax.com/reddo/70117%5Ebird1.mp3";
        public readonly static string CatCallSound = Uri.App("pg/gigs/68_second_timer/Sounds/CatCall.mp3");
        public readonly static string BeginSound = Uri.App("pg/gigs/68_second_timer/Sounds/Begin.mp3");
        public readonly static string DoneSound = Uri.App("pg/gigs/68_second_timer/Sounds/Done.mp3");

        [PreserveName, PreserveCase]
        public static void Page()
        {
            if (!ThemeYoshi.IsAdmin)
                return;

            if (Storage.GetLocal(QuarterBellKey) == null)
                Storage.SetLocal(QuarterBellKey, true);
            QuarterBell = (bool)Storage.GetLocal(QuarterBellKey);

            Sound0 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", BeginSound);
            Sound1 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", BellSound);
            Sound2 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", DoneSound);
            Sound4 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", CatCallSound);

            Indicator = jQuery.Select("#Indicator")
                .Click(delegate { StopSound(); })
                .Html("")
                ;

            jQuery.Select("#StartTimerBtn")
                .Click(StartTimerEv)
                ;

            jQuery.Select("#QuarterBell")
                .Click(QuarterBellEv)
                .Html(QuarterBell ? "ON" : "OFF")
                .CSS(new Dictionary("cursor", "pointer"))
                ;

        }

        public static void StartTimerEv(jQueryEvent e)
        {
            TimerGadgetOptions tgo = new TimerGadgetOptions();
            tgo.Trigger = Trigger;
            tgo.Start = Start;
            tgo.Stop = Stop;
            tgo.Cancel = Cancel;
            tgo.Element = Indicator;
            tgo.Interval = 1000;
            tgo.Duration = e.ShiftKey ? 4000 : 68000;
            tgo.Marks = e.ShiftKey ? (List<int>)Script.Literal("[2]") : (List<int>)Script.Literal("[17,34,51]");

            if (Running)
            {
                //ThemeYoshi.ReloadPage();
                tgo.Done = true;
                if (Timer != null)
                    Timer.Cancel();
                else
                    Cleanup();
                return;
            }

            Running = true;
            jQuery.Select("#StartTimerBtn").Html("reset timer");

            Play(Sound0, 1);
            Waiter = Window.SetTimeout(delegate { Timer = new TimerGadget(tgo); }, 6200);
        }

        public static void Play(jQueryObject sound, double volume)
        {
            Script.Literal("{0}.volume = {1}", sound[0], volume);
            Script.Literal("{0}.play()", sound[0]);
        }

        public static void Trigger(TimerGadgetOptions tgo)
        {
            tgo.Element.Html(tgo.Tick.ToString());
            if (QuarterBell && tgo.Marks.Contains(tgo.Tick))
                Play(Sound1, .1);
        }

        public static void Start(TimerGadgetOptions tgo)
        {
            Indicator.Html("0");
        }

        public static void Stop(TimerGadgetOptions tgo)
        {
            Indicator.Html("");
            StopSound();
        }

        public static void StopSound()
        {
            Play(Sound2, .75);
            Waiter = Window.SetTimeout(delegate
            {
                Play(Sound4, .1);
                Waiter = Window.SetTimeout(Cleanup, 1000);
            }, 3500);
        }

        public static void Cancel(TimerGadgetOptions tgo)
        {
            Cleanup();
        }

        public static void Cleanup()
        {
            Window.ClearTimeout(Waiter);
            Timer = null;
            jQuery.Select("#StartTimerBtn").Html("start timer");
            Indicator.Html("");
            Running = false;
        }

        public static void QuarterBellEv(jQueryEvent e)
        {
            QuarterBell = !QuarterBell;
            jQuery.Select("#QuarterBell").Html(QuarterBell ? "ON" : "OFF");
            Storage.SetLocal(QuarterBellKey, QuarterBell);
        }
    }
}
